package com.aramco.fion.model.presentation
{
	import com.aramco.fion.event.LoadQueryEvent;
	import com.aramco.fion.util.BSPReportDataModelOO;
	import com.aramco.fion.view.componentsManagement.ChartWizard;
	import com.aramco.fion.view.componentsManagement.NewChartWizard;
	import com.aramco.fion.view.componentsManagement.NewChartWizardStep01;
	import com.aramco.fion.view.componentsManagement.PreviewChart;
	import com.aramco.fion.view.componentsManagement.SeriesAndAxis;
	import com.aramco.fion.vo.Callbacks;
	import com.aramco.fion.vo.DashboardVo;
	import com.aramco.fion.vo.FionVo;
	import com.aramco.fion.vo.GroupViewVO;
	import com.aramco.fion.vo.SummaryViewVO;
	import com.aramco.fion.vo.chartWizard.FionAreaSeries;
	import com.aramco.fion.vo.chartWizard.FionBarSeries;
	import com.aramco.fion.vo.chartWizard.FionColumnSeries;
	import com.aramco.fion.vo.chartWizard.FionLineSeries;
	import com.aramco.fion.vo.chartWizard.FionPieSeries;
	
	import flash.display.DisplayObject;
	import flash.events.IEventDispatcher;
	
	import mx.charts.AreaChart;
	import mx.charts.BarChart;
	import mx.charts.CategoryAxis;
	import mx.charts.ColumnChart;
	import mx.charts.Legend;
	import mx.charts.LineChart;
	import mx.charts.PieChart;
	import mx.charts.chartClasses.CartesianChart;
	import mx.charts.chartClasses.ChartBase;
	import mx.charts.chartClasses.Series;
	import mx.charts.series.AreaSeries;
	import mx.charts.series.BarSeries;
	import mx.charts.series.ColumnSeries;
	import mx.charts.series.LineSeries;
	import mx.charts.series.PieSeries;
	import mx.collections.ArrayCollection;
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.events.IndexChangedEvent;
	import mx.rpc.events.FaultEvent;
	
	import spark.components.Panel;
	
	public class NewChartWizardPresentationModel
	{
		public function NewChartWizardPresentationModel()
		{
		}
		
		[Dispatcher]
		public var dispatcher:IEventDispatcher;
		
		public var queryName:String ="ZAD_MAT_01";
		public var chartType:String = "BarChart";
		public var series:ArrayCollection = new ArrayCollection();
	
		
		public var axis:Object;
		public var view:NewChartWizard;
		public var type:String;
		public var showDataTips:Boolean = true;
		public var showLengend:Boolean = false;
		public var legendDirection:String = "horizontal";
		
		public var chartWidth:Number = 100;
		public var chartHeight:Number = 100;
		
		
		public var seriesColors:Array = new Array();
		
		//Line Chart Only
		public var interpolateValues:Boolean= true;
		public var form:String;
		
		//Pie Chart Only
		public var labelPlacement:String;
		
		[Bindable]
		public var queryOutputMeta:ArrayCollection ;
		public var queryOutput:ArrayCollection ;
		
		[ViewAdded]
		public function viewAdded( view : NewChartWizard ) : void
		{
			this.view = view;
		}
		
	
			
		public function loadQueryMeta(queryName:String):void
		{
			if(queryName != null && queryName!="")
			{
				var handlers : Callbacks = new Callbacks(onResult_loadData);
				var myEvent:LoadQueryEvent = new LoadQueryEvent(LoadQueryEvent.LOAD_XML_REPORT_EVENT,handlers);
				myEvent.queryName = queryName;
				dispatcher.dispatchEvent(myEvent);
			}
		}
		
		private function onResult_loadData(data:BSPReportDataModelOO):void
		{
			view.myViewStack.selectedIndex = 1;
			queryOutputMeta = data.metaData;
			queryOutput = data.repotData;
		}
		
		
		public function selectChartType(chartType:String, view:SeriesAndAxis):void
		{
			this.chartType = chartType;
			view.currentState = chartType;
			/*if(chartType == "PieChart")
				view.currentState = "PieChart";
			else
				view.currentState = "";*/
		}
		
		public function next(obj:DisplayObject):void
		{
		/*	if(obj is SeriesAndAxis)
			{
				series = (obj as SeriesAndAxis).dg1.getSelectedRecords();
				axis =  (obj as SeriesAndAxis).dg2.getSelectedRecords();
				chartType = (obj as SeriesAndAxis).chartTypeDropDownList.selectedItem.value;
				view.myViewStack.selectedIndex = 2;
			}
			
			else if(obj is PreviewChart)
			{
				
			}*/
		}
		
		public function prev():void
		{
			
		}
		
		
		private function generateSeries(obj:Object):*
		{
			var tmpSeries:Series;
			if(obj.seriesType == "BarSeries")
			{
				tmpSeries = new BarSeries();
				(tmpSeries as BarSeries).xField = obj['variableName'];
				tmpSeries.displayName = obj['variableDesc'];
			}
			
			else if(obj.seriesType == "ColumnSeries")
			{
				tmpSeries = new ColumnSeries();
				(tmpSeries as ColumnSeries).yField = obj['variableName'];
				tmpSeries.displayName = obj['variableDesc'];
			}
			
			else if(obj.seriesType == "AreaSeries")
			{
				tmpSeries = new AreaSeries();
				(tmpSeries as AreaSeries).yField = obj['variableName'];
				tmpSeries.displayName = obj['variableDesc'];
			}
			
			else if(obj.seriesType == "LineSeries")
			{
				tmpSeries = new LineSeries();
				(tmpSeries as LineSeries).yField = obj['variableName'];
				tmpSeries.displayName = obj['variableDesc'];
			}
			else if(obj.seriesType == "PieSeries")
			{
				tmpSeries = new PieSeries();
				(tmpSeries as PieSeries).field = obj['variableName'];
				tmpSeries.displayName = obj['variableDesc'];
			}
			return tmpSeries;
		}
		
		public function generateChart(outputView:spark.components.Panel):void
		{
			
		}
		
		
		public function refreshChart(outputView:spark.components.Panel):void
		{
			var chart:ChartBase;
			var tmpSeries:Series;
			axis = view.chartWizardStep01.axisGird.selectedRecord;
			
			if(chartType == "BarChart")
			{
				chart = new BarChart();
				chart.percentHeight = chartHeight;
				chart.percentWidth = chartWidth;
				(chart as BarChart).type = type;
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					mySeries.push(generateSeries(series[i]));
				}
				
			
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as BarChart).verticalAxis = myAxis;	
			}
			
			else if(chartType == "ColumnChart")
			{
				chart = new ColumnChart();
				chart.percentHeight = chartHeight;
				chart.percentWidth = chartWidth;
				(chart as ColumnChart).type = type;
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					mySeries.push(generateSeries(series[i]));
				}
				
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as ColumnChart).horizontalAxis = myAxis;
				
			}
			
			else if(chartType == "AreaChart")
			{
				chart = new AreaChart();
				chart.percentHeight = chartHeight;
				chart.percentWidth = chartWidth;
				(chart as AreaChart).type = type;
				
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					mySeries.push(generateSeries(series[i]));
				}
				
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as AreaChart).horizontalAxis = myAxis;
			}
			
			else if(chartType == "LineChart")
			{
				chart = new LineChart();
				chart.percentHeight = chartHeight;
				chart.percentWidth = chartWidth;
				var mySeries:Array =new Array();
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					mySeries.push(generateSeries(series[i]));
				}
			
				var myAxis:CategoryAxis = new CategoryAxis();
				myAxis.dataProvider = queryOutput;
				myAxis.categoryField = axis['FIELDNAME'];
				
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
				(chart as LineChart).horizontalAxis = myAxis;
			}
			
			else if(chartType == "PieChart")
			{
				chart = new PieChart();
				chart.percentHeight = chartHeight;
				chart.percentWidth = chartWidth;
				var mySeries:Array =new Array();				
				
				for(var i:int = 0 ; i < series.length; i++)
				{
					mySeries.push(generateSeries(series[i]));
				}
				
				chart.showDataTips = showDataTips;			
				chart.dataProvider = queryOutput;
				chart.series = mySeries;
			}
			
			outputView.removeAllElements();
			outputView.addElement(chart);
			if(showLengend)
			{
				var legned:Legend = new Legend();
				legned.direction = legendDirection;
				legned.dataProvider = chart;
				outputView.addElement(legned);
			}
		}
		
		public function chartType_changeHandler(value:String):void
		{
			this.chartType = value;
		}
	
		
		
		public function addDataField(obj:Object, seriesType:String):void
		{
			if(seriesType == "BarSeries")
			{
				var mySeries:FionBarSeries = new FionBarSeries();
				
				mySeries.variableDesc = obj['SCRTEXT_L'];
				mySeries.variableName = obj['FIELDNAME'];
				mySeries.seriesType = "BarSeries";
				series.addItem(mySeries);
			}
			
			else if(seriesType == "AreaSeries")
			{
				var mySeries2:FionAreaSeries = new FionAreaSeries();
				mySeries2.variableDesc = obj['SCRTEXT_L'];
				mySeries2.variableName = obj['FIELDNAME'];
				mySeries2.seriesType = "AreaSeries";
				series.addItem(mySeries2);
			}
			
			else if(seriesType == "ColumnSeries")
			{
				var mySeries3:FionColumnSeries = new FionColumnSeries();
				mySeries3.variableDesc = obj['SCRTEXT_L'];
				mySeries3.variableName = obj['FIELDNAME'];
				mySeries3.seriesType = "ColumnSeries";
				series.addItem(mySeries3);
			}
			
			else if(seriesType == "LineSeries")
			{
				var mySeries4:FionLineSeries = new FionLineSeries();
				mySeries4.variableDesc = obj['SCRTEXT_L'];
				mySeries4.variableName = obj['FIELDNAME'];
				mySeries4.seriesType = "LineSeries";
				series.addItem(mySeries4);
			}
			
			else if(seriesType == "PieSeries")
			{
				var mySeries5:FionPieSeries = new FionPieSeries();
				mySeries5.variableDesc = obj['SCRTEXT_L'];
				mySeries5.variableName = obj['FIELDNAME'];
				mySeries5.seriesType = "PieSeries";
				series.addItem(mySeries5);
			}
		}
		
		public function deleteDataField(obj:Object):void   
		{
			series.removeItemAt(series.getItemIndex(obj));
		}
	}
}